<template>
  <div class="login" v-if="isShow">
    <div class="login-form">
      <p>用户登录</p>
      <a-form
        layout="horizontal"
        :model="loginParams"
        @finish="handleFinish"
        @finishFailed="handleFinishFailed"
      >
        <a-form-item v-show="userType == '0'">
          <a-input v-model:value="loginParams.username" placeholder="请输入用户名">
            <template #prefix>
              <UserOutlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item name="password">
          <a-input v-model:value="loginParams.password" type="password" placeholder="请输入密码">
            <template #prefix>
              <LockOutlined style="color: rgba(0, 0, 0, 0.25)" />
            </template>
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-button
            class="login-form-submit"
            type="primary"
            html-type="submit"
            :disabled="loginParams.username === '' || loginParams.password === ''"
          >
            登 录
          </a-button>
        </a-form-item>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue';
import { ValidateErrorEntity } from 'ant-design-vue/es/form/interface';
import { reactive, UnwrapRef, ref } from 'vue';
import Api, { LoginParams } from '@/api/index';
import { useRouter } from 'vue-router';
import { GlobalState } from '@/store/index';

const userType = ref('0')
const isShow = ref(true)
const store = GlobalState()
const router = useRouter()
const loginParams: UnwrapRef<LoginParams> = reactive({
  mobile: '',
  password: '',
  username: ''
});
const handleFinish = async (values: LoginParams) => {
  loginParams.mobile = ''
  let data = await Api.login(loginParams)
  console.log(data)
  const { id } = data;
  window.sessionStorage.setItem('token', id + '')
  store.setUserInfo(data)
  router.push('/index')
};
const handleFinishFailed = (errors: ValidateErrorEntity<LoginParams>) => {
  console.log(errors);
};
</script>

<style lang="less" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f2f2f2;
  height: 100vh;
  &-form {
    width: 400px;
    padding: 30px;
    box-shadow: 2px 2px 10px #808080;
    border-radius: 8px;
    p {
      text-align: center;
      font-family: '个性字体';
      font-size: 28px;
    }
    &-submit {
      width: 100%;
    }
    .user-type {
      text-align: center;
    }
    .register {
      text-align: right;
      font-size: 12px;
      color: @link-color;
      cursor: pointer;
      margin-top: 8px;
    }
  }
}
</style>
